package com.单词的逆序转变;

public class Solution {

	public String reverseSentence(String A, int n) {
		// 第一步将整个字符串都逆置
		char a[] = A.toCharArray();
		reserve(0, n - 1, a);
		
		int start = 0, end = 0;
		int i,j;
		for ( i = 0, j = 0; i < n; i++) {
			
			if (a[i] == ' ') {
				start = j;
				end = i - 1;
				
				reserve(start, end, a);
				
				j = i + 1;
			}
		}
		reserve(j,n-1,a);
		
		String result = new String(a);
		return result;
	}

	public void reserve(int start, int end, char[] a) {
		while(start<end) {
			char temp = a[start];
			a[start] = a[end];
			a[end] = temp;
			start++;
			end--;
		}

	}

}
